package com.lam.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lam.dormitory.util.ShiroUtils;
import com.lam.dormitory.entity.DormitoryService;
import com.lam.dormitory.entity.Student;
import com.lam.dormitory.service.IDormitoryServiceService;
import com.lam.dormitory.service.IStudentService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.ChartHygieneVo;
import com.lam.dormitory.vo.ChartVo;
import com.lam.dormitory.vo.DormitoryServiceDetailVo;
import com.lam.dormitory.vo.DormitoryServiceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Api(tags = "宿舍服务接口")
@RestController
@RequestMapping("/dormitoryService")
public class DormitoryServiceController {
    @Autowired
    private IDormitoryServiceService dormitoryServiceService;
    @Autowired
    private IStudentService studentService;

    /**
     * 添加服务
     *
     * @param dormitoryService
     * @return
     */
    @ApiOperation(value = "添加服务", notes = "添加服务")
    @PostMapping("/add")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate addDorService(DormitoryService dormitoryService) {
        QueryWrapper<DormitoryService> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DormitoryService::getDorId, dormitoryService.getDorId()).eq(DormitoryService::getDsMonth, dormitoryService.getDsMonth());
        DormitoryService one = dormitoryServiceService.getOne(wrapper);
        if (one != null) {
            return new ResponseTemplate(500, "该月该宿舍的服务表单已存在");
        } else {
            dormitoryService.setStatus(1);
            boolean save = dormitoryServiceService.save(dormitoryService);
            if (save) {
                return new ResponseTemplate(200, "添加成功");
            }
            return new ResponseTemplate(500, "添加失败");
        }
    }

    /**
     * 删除服务
     *
     * @param dsId
     * @return
     */
    @ApiOperation(value = "删除服务", notes = "根据dsId删除服务")
    @PostMapping("/delete")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate deleteDorService(Long dsId) {
        QueryWrapper<DormitoryService> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DormitoryService::getDsId, dsId);
        boolean remove = dormitoryServiceService.remove(wrapper);
        if (remove) {
            return new ResponseTemplate(200, "删除成功");
        }
        return new ResponseTemplate(500, "删除失败");
    }

    /**
     * 修改服务信息
     *
     * @param dormitoryService
     * @return
     */
    @ApiOperation(value = "修改服务信息", notes = "根据dsId修改服务信息")
    @PostMapping("/edit")
    @RequiresRoles(value = {"admin", "superadmin"}, logical = Logical.OR)
    public ResponseTemplate editDorService(DormitoryService dormitoryService) {
        QueryWrapper<DormitoryService> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DormitoryService::getDsId, dormitoryService.getDsId());
        boolean update = dormitoryServiceService.update(dormitoryService,wrapper);
        if (update) {
            return new ResponseTemplate(200, "修改成功");
        }
        return new ResponseTemplate(500, "修改失败");
    }

    /**
     * 服务信息列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "服务信息列表", notes = "可根据dorId,月份范围查询服务信息列表,pageNum 默认为 1，pageSize默认为 0")
    @PostMapping("/list")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate pageDorServices(Long dorId,String startTime, String endTime, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        DormitoryService dormitoryService = new DormitoryService();
        dormitoryService.setDorId(dorId);
        dormitoryService.setStartTime(startTime);
        dormitoryService.setEndTime(endTime);
        List<DormitoryServiceVo> dormitoryServiceVoList = dormitoryServiceService.selectDsVo(dormitoryService);
        PageInfo<DormitoryServiceVo> pageInfo = new PageInfo<>(dormitoryServiceVoList);
        return new ResponseTemplate(200, "服务信息列表返回成功", dormitoryServiceVoList, pageInfo.getTotal());
    }

    /**
     * 服务信息详情
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "服务信息详情", notes = "可根据dsId")
    @PostMapping("/detail")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate pageDorServices(DormitoryService dormitoryService, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "0") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DormitoryServiceDetailVo> dormitoryServiceVoList = dormitoryServiceService.selectDsDetailVo(dormitoryService);
        PageInfo<DormitoryServiceDetailVo> pageInfo = new PageInfo<>(dormitoryServiceVoList);
        return new ResponseTemplate(200, "服务信息列表返回成功", dormitoryServiceVoList, pageInfo.getTotal());
    }
//
//    /**
//     * 根据当前学生userId查询宿舍服务信息
//     *
//     * @return
//     */
//    @ApiOperation(value = "用户服务信息列表(学生userId)", notes = "可根据当前userId或者填入userId查询宿舍服务信息")
//    @PostMapping("/student/list")
////    @RequiresPermissions("role:list")
//    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
//    public ResponseTemplate DorServiceByUserId(Long userId) {
//        if (userId == null) {
//            userId = ShiroUtils.getUserInfo().getUserId();
//        }
//        QueryWrapper<Student> wrapper = new QueryWrapper<>();
//        wrapper.lambda().eq(Student::getUserId, userId);
//        Student student = studentService.getOne(wrapper);
//        List<DormitoryServiceVo> dormitoryServiceVoList = dormitoryServiceService.selectDsVoByDorId(student.getDorId());
//        return new ResponseTemplate(200, "查询成功", dormitoryServiceVoList);
//    }

    /**
     * 根据条件展示统计图
     * @param startTime
     * @param endTime
     * @param buildingNo
     * @param type
     * @return
     */
    @ApiOperation(value = "根据条件展示统计图", notes = "根据条件展示统计图")
    @PostMapping("/chart")
//    @RequiresPermissions("role:list")
    @RequiresRoles(value = {"admin", "superadmin", "student"}, logical = Logical.OR)
    public ResponseTemplate dorServiceChart(String startTime, String endTime, Integer buildingNo, Integer type) {
        DormitoryService dormitoryService = new DormitoryService();
        dormitoryService.setStartTime(startTime);
        dormitoryService.setEndTime(endTime);
        if (type == 1) {
            List<ChartVo> chartVos = dormitoryServiceService.monthTotalChart();
            return new ResponseTemplate(200, "查询成功", chartVos);
        } else if (type == 2) {
            List<ChartVo> chartVos = dormitoryServiceService.monthBuildingTotalChart(dormitoryService);//月份范围
            return new ResponseTemplate(200, "查询成功", chartVos);
        } else if (type == 3) {
            dormitoryService.setBuildingNo(buildingNo);
            List<ChartVo> chartVos = dormitoryServiceService.monthBuildingDorNoTotal(dormitoryService);//宿舍楼范围，月份范围
            return new ResponseTemplate(200, "查询成功", chartVos);
        }else if (type == 4) {
            List<ChartHygieneVo> chartVos = dormitoryServiceService.monthHygieneTotal(dormitoryService);//月份范围
            return new ResponseTemplate(200, "查询成功", chartVos);
        }else if (type == 5) {
            dormitoryService.setBuildingNo(buildingNo);
            List<ChartHygieneVo> chartVos = dormitoryServiceService.monthBuildingDorNoHygieneTotal(dormitoryService);//宿舍楼范围，月份范围
            return new ResponseTemplate(200, "查询成功", chartVos);
        }
        return new ResponseTemplate(500, "查询失败");
    }
}
